package com.shangxia.dog.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;

/**
 * @author zhouya24
 * @date 2021/11/18 15:26
 * @apiNote
 */

@Slf4j
public class CodeGeneratorUtil {


    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/dog?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC";
        String targetPath = "C://study//java//IdeaProjects//sxzoo//dog//src//main//java";
        String targetMapperPath = "C://study//java//IdeaProjects//sxzoo//dog//src//main//resources//mapper//base";

        FastAutoGenerator.create(url, "root", "root")
                .globalConfig(builder -> {
                    builder.author("shangxia") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .dateType(DateType.TIME_PACK)//数据库时间匹配java类型
                            .outputDir(targetPath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.shangxia.dog") // 设置父包名
                            .moduleName("base") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, targetMapperPath)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    StrategyConfig build = builder
                            .addInclude("position_") // 设置需要生成的表名/
//                            .addExclude("admin","menu","admin_role","role","role_menu")
                            .entityBuilder()
                            .enableLombok()
                            .superClass("")
//                              .disableSerialVersionUID()
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO).build();
                    // .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    build.mapperBuilder().enableBaseResultMap()
                            .enableBaseColumnList();

                    build.controllerBuilder().enableRestStyle().build();

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
